package com.customer.hystrix.controller;

import com.customer.hystrix.servie.HystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author：Administrator
 * @Date：2022/7/13/0013 20:30
 * @Versiion：1.0
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "globalFallBack")
public class HystrixController extends BaseController{

    @Resource
    private HystrixService hystrixService;

    @GetMapping("/paymentOk")
    /*未配置降级的方法 就使用的全局DefaultProperties配置的降级方法*/
    @HystrixCommand
    public String paymentOk(){
        //触发全局降级
        //int i = 10 / 0;
        return hystrixService.paymentOk1();
    }

    @GetMapping("/paymentTimeout")
    //HystrixCommandProperties 中有以下配置内容
    @HystrixCommand(fallbackMethod = "paymentTimeoutFallBackMethod",commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "6000")
    })
    public String paymentTimeout(){
        return hystrixService.paymentTimeout1();
    }

    public String paymentTimeoutFallBackMethod(){
        return "不想等了 接口返回太慢了";
    }
}
